What is Host in ASP.NET Core?
What is Host in ASP.NET Core?
392
06-Mar-2023
Updated on 08-Jul-2023
Aryan Kumar
08-Jul-2023In ASP.NET Core, a host is an object that is responsible for running an ASP.NET Core application. It is responsible for starting the application, handling requests, and shutting down the application when it is finished.
The host is implemented as a class that implements the
IHostinterface. TheIHostinterface has a number of methods that are used to start, handle requests, and shut down the application.The following is an example of a host class:
C#
To create a host object, you can use the
CreateHostBuilder()method of theMicrosoft.AspNetCore.Hosting.HostingBuilderclass. TheCreateHostBuilder()method takes a configuration object as its parameter. The configuration object can be used to configure the application, such as the port number that the application will listen on.The following is an example of how to create a host object:
C#
Once you have created a host object, you can start it by calling the
Start()method. TheStart()method will start the application and listen for requests.To handle a request, you can call the
HandleRequest()method of the host object. TheHandleRequest()method takes aHttpContextobject as its parameter. TheHttpContextobject contains information about the request, such as the URL, the headers, and the body of the request.To shut down the host object, you can call the
Shutdown()method. TheShutdown()method will shut down the application and release any resources that it is using.